package ar.com.miguelgriffa.eclipse.wizards.secureproperties;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ResourceSelectionDialog;

public class SecurePropertiesWizardPage extends WizardPage {
//	private Text containerText;

	private Text plainFileText;
	
	private Text secureFileText;

	private ISelection selection;

	/**
	 * Constructor for SampleNewWizardPage.
	 * 
	 * @param pageName
	 */
	public SecurePropertiesWizardPage(ISelection selection) {
		super("wizardPage");
		setTitle("Multi-page Editor File");
		setDescription("This wizard creates a new file with *.properties extension that can be opened by a multi-page editor.");
		this.selection = selection;
	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 3;
		layout.verticalSpacing = 9;

		// File to encrypt
		Label label = new Label(container, SWT.NULL);
		label.setText("&Plain File name:");

		plainFileText = new Text(container, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		plainFileText.setLayoutData(gd);
		plainFileText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
				secureFileText.setText(plainFileText.getText()+"_secure");
			}
		});
		Button button = new Button(container, SWT.PUSH);
		button.setText("Browse...");
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				handleBrowseFile();
			}
		});

		label = new Label(container, SWT.NULL);
		label.setText("&SecureFile name:");
		secureFileText = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		secureFileText.setLayoutData(gd);
		secureFileText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});

		initialize();
		dialogChanged();
		setControl(container);
	}

	private void initialize() {
		if (selection != null && selection.isEmpty() == false
				&& selection instanceof IStructuredSelection) {
			IStructuredSelection ssel = (IStructuredSelection) selection;
			if (ssel.size() > 1)
				return;
			Object obj = ssel.getFirstElement();
			if (obj instanceof IResource) {
				IContainer container;
				if (obj instanceof IContainer)
					container = (IContainer) obj;
				else
					container = ((IResource) obj).getParent();
//				containerText.setText(container.getFullPath().toString());
			}
		}
		plainFileText.setText(getPlainFileText());
	}

	private String getPlainFileText() {
		if (this.selection!=null) {
			Object fe = ((IStructuredSelection )this.selection).getFirstElement();
			if (fe instanceof IFile) {
				return ((IFile)fe).getFullPath().toString();
			}
			System.out.println("Selection: "+ fe);
			return "";
		}
		return "";
	}

	private void handleBrowseFile() {
		ResourceSelectionDialog dialog = new ResourceSelectionDialog(getShell(),  ResourcesPlugin.getWorkspace().getRoot(), "Select file" );
		if (dialog.open() == ResourceSelectionDialog.OK) {
			Object[] result = dialog.getResult();
			if (result.length == 1) {
				plainFileText.setText(((IResource) result[0]).getFullPath().toString());
			}
		}
	}


	private void dialogChanged() {
		String srcFileName = getPlainFileName();
		String tgtFileName = getSecureFileName();
		
		if (srcFileName.length() == 0) {
			updateStatus("Plain File name must be specified");
			return;
		}
		int dotLoc = srcFileName.lastIndexOf('.');
		if (dotLoc != -1) {
			String ext = srcFileName.substring(dotLoc + 1);
			if (ext.equalsIgnoreCase("properties") == false) {
				updateStatus("Plain File extension must be \"properties\"");
				return;
			}
		}

		if (tgtFileName.length() == 0) {
			updateStatus("Target File name must be specified");
			return;
		}

		updateStatus(null);
	}

	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}

	public String getPlainFileName() {
		if (plainFileText==null)
			return "";
		return plainFileText.getText();
	}

	public String getSecureFileName() {
		if (secureFileText==null)
			return "";
		return secureFileText.getText();
	}
}